package com.donger.business.form.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.donger.auth.utils.SecurityUtils;
import com.donger.business.form.entity.SysFormDesign;
import com.donger.business.form.query.FormDesignDto;
import com.donger.business.form.query.FormDesignJson;
import com.donger.business.form.query.FormDesignQuery;
import com.donger.business.form.service.SysFormDesignService;
import com.donger.common.core.exception.BizException;
import com.donger.common.core.utils.Res;
import com.donger.common.core.utils.Result;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @author 7326
 */
@RestController
@RequestMapping("/design/form")
@AllArgsConstructor
public class SysFormDesignController {

    private final SysFormDesignService sysFormDesignService;


    @GetMapping("/page")
    public Result page(Page<SysFormDesign> page, FormDesignQuery query) {
        sysFormDesignService.page(page, Wrappers.<SysFormDesign>lambdaQuery()
                .select(SysFormDesign::getId, SysFormDesign::getCode, SysFormDesign::getName,
                        SysFormDesign::getCreateTime, SysFormDesign::getUpdateTime,
                        SysFormDesign::getVersion)
                .like(StrUtil.isNotBlank(query.getName()), SysFormDesign::getName, query.getName())
                .like(StrUtil.isNotBlank(query.getCode()), SysFormDesign::getCode, query.getCode())
        );
        return Res.ok(page);
    }

    @GetMapping("/info/{id}")
    public Result<SysFormDesign> info(@PathVariable("id") Long id) {
        SysFormDesign entity = sysFormDesignService.getById(id);
        return Res.ok(entity);
    }

    @GetMapping("/code/{code}")
    public Result<SysFormDesign> code(@PathVariable("code") String code) {
        SysFormDesign entity = sysFormDesignService.getOne(Wrappers.<SysFormDesign>lambdaQuery()
                .eq(SysFormDesign::getCode, code)
        );
        return Res.ok(entity);
    }

    @PostMapping("/save")
    public Result<Boolean> save(@RequestBody FormDesignDto entity) {
        SysFormDesign formDesign = null;
        // 校验表单编码是否重复
        formDesign = sysFormDesignService.getOne(Wrappers.<SysFormDesign>lambdaQuery().eq(SysFormDesign::getCode, entity.getCode()));
        if (formDesign != null) {
            throw new BizException("表单编码不能重复");
        }
        formDesign = new SysFormDesign();
        BeanUtil.copyProperties(entity, formDesign);
        formDesign.setVersion(1L);
        formDesign.setCreateName(SecurityUtils.getUser().getUsername());
        formDesign.setCreateTime(LocalDateTime.now());
        sysFormDesignService.saveForm(formDesign);
        return Res.ok();
    }

    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody FormDesignDto entity) {
        SysFormDesign formDesign = null;
        // 校验表单编码是否重复
        formDesign = sysFormDesignService.getOne(Wrappers.<SysFormDesign>lambdaQuery()
                .eq(SysFormDesign::getCode, entity.getCode())
                .ne(SysFormDesign::getId, entity.getId())
        );
        if (formDesign != null) {
            throw new BizException("表单编码不能重复");
        }
        formDesign = new SysFormDesign();
        BeanUtil.copyProperties(entity, formDesign);
        sysFormDesignService.saveForm(formDesign);
        return Res.ok();
    }

    @PostMapping("/saveJson")
    public Result<Boolean> saveJsonData(@RequestBody FormDesignJson entity) {
        sysFormDesignService.saveJson(entity.getId(), entity.getJsonData());
        return Res.ok();
    }

    @DeleteMapping("/del/{id}")
    public Result<Boolean> delete(@PathVariable("id") Long id) {
        sysFormDesignService.removeById(id);
        return Res.ok();
    }


}
